package com.clear.club.circle.server.controller;


import com.alibaba.fastjson.JSON;
import com.clear.club.circle.api.common.PageResult;
import com.clear.club.circle.api.common.Result;
import com.clear.club.circle.api.req.GetMomentsReq;
import com.clear.club.circle.api.req.RemoveMomentCircleReq;
import com.clear.club.circle.api.req.SaveMomentCircleReq;
import com.clear.club.circle.api.vo.ShareMomentVO;
import com.clear.club.circle.server.entity.po.ShareCircle;
import com.clear.club.circle.server.entity.po.ShareMoment;
import com.clear.club.circle.server.sensitive.WordFilter;
import com.clear.club.circle.server.service.IShareMomentService;
import com.clear.club.circle.server.service.ShareCircleService;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 动态信息 前端控制器
 * </p>
 *
 * @author 宁洪康
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/circle/share/moment")
@RequiredArgsConstructor
@Slf4j
public class ShareMomentController {

    private final ShareCircleService shareCircleService;

    private final IShareMomentService shareMomentService;

    private final WordFilter wordFilter;

    /**
     * 发布内容
     */
    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody SaveMomentCircleReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("发布动态内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getCircleId()), "圈子id不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getContent()) || !CollectionUtils.isEmpty(req.getPicUrlList()), "发布内容不能为空！");
            // 判断圈子是否存在
            ShareCircle circle = shareCircleService.getById(req.getCircleId());
            Preconditions.checkArgument(Objects.nonNull(circle) && circle.getId() != -1, "关联圈子非法！");
            // 敏感词检测
            if (Objects.nonNull(req.getContent())){
                wordFilter.check(req.getContent());
            }
            Boolean result = shareMomentService.saveMoment(req);
            if (log.isInfoEnabled()) {
                log.info("发布动态内容{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发布动态内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("发布动态内容异常！");
        }
    }

    /**
     * 分页查询动态内容
     */
    @PostMapping(value = "/getMoments")
    public Result<PageResult<ShareMoment>> getMoments(@RequestBody GetMomentsReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("分页查询动态内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getCircleId()), "圈子id不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getPageInfo()), "分页信息不能为空！");
            PageResult<ShareMomentVO> result = shareMomentService.getMoments(req);
            if (log.isInfoEnabled()) {
                log.info("分页查询动态内容出参{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询动态内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("分页查询动态内容！");
        }
    }

    /**
     * 删除动态内容
     */
    @PostMapping(value = "/remove")
    public Result<Boolean> remove(@RequestBody RemoveMomentCircleReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("删除动态内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "参数不能为空！");
            Boolean result = shareMomentService.removeMoment(req);
            if (log.isInfoEnabled()) {
                log.info("删除动态内容{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除动态内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("删除动态内容异常！");
        }
    }
}
